import {showToast} from './index';

/**
 * 微信朋友：new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
 * 微信朋友圈：new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
 * 微博：new ComponentName("com.sina.weibo", "com.sina.weibo.composerinde.ComposerDispatchActivity");
 * QQ朋友：new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
 * QQ空间：new ComponentName("com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
 */


/**
 * 实现分享多图到朋友圈
 */

/** 分享到 0:微信好友、1:微信朋友圈
 * 分享到朋友圈时失效
 * const shareto = ['com.tencent.mm.ui.tools.ShareImgUI','com.tencent.mm.ui.tools.ShareToTimeLineUI'];
 */
const shareto = 'com.tencent.mm.ui.tools.ShareImgUI';



/** 微信分享图片
 * param1 shareto数组中的一个
 * param2 图片数组
 * param3 描述信息
 */
function weixin_share_mul_pic(ex, pics, description) {
	var Intent = plus.android.importClass('android.content.Intent');
	var ComponentName = plus.android.importClass('android.content.ComponentName');
	var ArrayList = plus.android.importClass('java.util.ArrayList');
	var Uri = plus.android.importClass('android.net.Uri');
	var Environment = plus.android.importClass('android.os.Environment');
	var File = plus.android.importClass('java.io.File');
	//var sdcardDir = plus.android.invoke(Environment.getExternalStorageDirectory(),'getAbsolutePath');  
	var intent = new Intent();
	var localComponentName = new ComponentName("com.tencent.mm", ex);
	intent.setComponent(localComponentName);
	intent.setAction("android.intent.action.SEND_MULTIPLE");
	intent.setType("image/*");
	var localArrayList = new ArrayList();
	for (var i = 0; i < pics.length; i++) {
		var filePath = pics[i]; //sdcardDir +   
		//console.log('filePath=' + filePath);  
		localArrayList.add(Uri.fromFile(new File(filePath)));
	}
	intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
	intent.putExtra(Intent.EXTRA_TEXT, description);
	var act = plus.android.runtimeMainActivity();
	act.startActivity(intent);
}



/** 微信分享视频
 * param1 shareto数组中的一个
 * param2 视频数据
 */
/* function weixin_share_video(ex, src) {
	var Intent = plus.android.importClass('android.content.Intent');
	var ComponentName = plus.android.importClass('android.content.ComponentName');
	var ArrayList = plus.android.importClass('java.util.ArrayList');
	var Uri = plus.android.importClass('android.net.Uri');
	var Environment = plus.android.importClass('android.os.Environment');
	var File = plus.android.importClass('java.io.File');
	//var sdcardDir = plus.android.invoke(Environment.getExternalStorageDirectory(),'getAbsolutePath');  
	var intent = new Intent();
	var localComponentName = new ComponentName("com.tencent.mm", ex);
	intent.setComponent(localComponentName);
	intent.setAction("android.intent.action.SEND_MULTIPLE");
	intent.setType("video/*");
	var localFile = Uri.fromFile(new File(src));
	intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localFile);
	intent.putExtra(Intent.EXTRA_TEXT, description);
	var act = plus.android.runtimeMainActivity();
	act.startActivity(intent);
} */


function weixin_share_video(src){
	var Intent = plus.android.importClass('android.content.Intent');
	var ComponentName = plus.android.importClass('android.content.ComponentName');
	var File = plus.android.importClass('java.io.File');
	var Uri = plus.android.importClass('android.net.Uri');
	var intent = new Intent(Intent.ACTION_SEND);
	var localComponentName = new ComponentName("com.tencent.mm", shareto);
	intent.setComponent(localComponentName);
	intent.setType("video/*");
	var localFile = new File(src);
	intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(localFile));
	//intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localFile);
	var act = plus.android.runtimeMainActivity();
	act.startActivity(intent);
}
	

//转发图片
export function myshare(picsUrl){
	var pictures  = [];  //图片数组
	let imgsLen = picsUrl.length;
	let count = 0;
	//下载图片
	uni.showLoading({
		title: '图片下载中...'
	});
	downloadPics();
	function downloadPics(){
		if(count < imgsLen){
			uni.downloadFile({
				url: picsUrl[count],
				success(res) {
					let tempFilePath = res.tempFilePath;
					pictures.push(plus.io.convertLocalFileSystemURL(tempFilePath));
					count++;
					downloadPics();
				},
				fail(){
					showToast('下载图片失败');
				}
			});
		}else{
			uni.hideLoading();
			weixin_share_mul_pic(shareto, pictures);
		}
	};
};


//转发视频
export function shareMyVideo(src){
	uni.showLoading({
		title: '视频下载中...'
	});
	var downloadTask = uni.downloadFile({
		url: src,
		success(res){
			showToast('下载完成');
			uni.hideLoading();
			let tempFilePath = res.tempFilePath;
			let video1 = plus.io.convertLocalFileSystemURL(tempFilePath);
			weixin_share_video(video1);
		},
		fail() {
			uni.hideLoading();
			showToast('下载失败');
		}
	});
	
	var myprecent = 0;
	downloadTask.onProgressUpdate((res) => {
		if(res.progress > myprecent){
			myprecent = res.progress;
			uni.showLoading({
				title: myprecent+'%'
			});
		};
	});
}